﻿// HSS.Interlink.Web.BaseHandler.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2012
// ----------------------------------------------------------------------------
// File:       BaseHandler.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink.Web
{
	#region Using Directives
	using System;
	using System.IO;
	using System.Reflection;
	using System.Security.Permissions;
	using System.Web;
	using System.Web.Hosting;
	using Microsoft.Win32;
	#endregion

	#region BaseHandler
	/// <summary>
	/// BaseHandler is the base implementation of a File download and upload
	/// handlers and handles the Protocol portion of a request.
	/// </summary>
	public abstract class BaseHandler
	{
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public BaseHandler()
		{
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the current HttpContext
		/// </summary>
		public HttpContext Context
		{
			get;
			private set;
		}
		/// <summary>
		/// Gets the current user name if the context user identity is available; otherwise an empty string.
		/// </summary>
		public string UserName
		{
			get
			{
				if (null != this.Context && null != this.Context.User && null != this.Context.User.Identity)
					return this.Context.User.Identity.Name;
				else
					return string.Empty;
			}
		}
		/// <summary>
		/// The query string parsed into it their individual properties
		/// </summary>
		public FileQuery Query
		{
			get;
			private set;
		}
		/// <summary>
		/// Gets the current file Name.
		/// </summary>
		public string FileName { get { return this.Query.FileName; } }
		#endregion

		#region Methods

		/// <summary>
		/// Gets the file Content-Type Description based on the file name and extension from the Windows Registry.
		/// </summary>
		/// <param name="fileName">The name of the file to get the Description for. (Example: file.ext)</param>
		/// <returns>The file description if found; otherwise a plain string containing 'User File'</returns>
		/// <exception cref="System.Security.SecurityException">A caller higher in the call stack 
		/// does not have the permission specified by the current instance.-or- A caller higher in the call stack has called 
		/// System.Security.CodeAccessPermission.Deny() on the current permission object.</exception>
		public virtual string GetContentTypeDescription(string fileName)
		{
			return InterlinkUtilities.GetContentTypeDescription(fileName);
		}
		/// <summary>
		/// Gets a folder relative to the ApplicationPhysicalPath.
		/// </summary>
		/// <param name="folderName">The name of the folder</param>
		/// <returns>The folder's full path</returns>
		public virtual string GetFolder(string folderName)
		{
			return InterlinkUtilities.GetFolder(folderName);
		}

		#region Abstract
		/// <summary>
		/// Validate the current request.
		/// </summary>
		/// <returns>true if the request is allowed; otherwise false</returns>
		public abstract bool IsAuthorized();
		#endregion

		#region Internal
		internal string CommonInit(HttpContext context, FileQuery fileQuery)
		{
			this.Context = context;
			this.Query = fileQuery;
			return fileQuery.JobId.ToString() + ".job";
		}
		internal bool IsAuthorizedInternal()
		{
			var isAuthorized = false;
			try
			{
				isAuthorized = this.IsAuthorized();
			}
			catch { isAuthorized = false; }
			if (!isAuthorized)
			{
				this.Context.Response.Clear();
				this.Context.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
				return false;
			}
			return true;
		}
		internal static bool CompareHash(byte[] hash1, byte[] hash2)
		{
			bool isEqual = false;

			if (hash1.Length == hash2.Length)
			{
				int i = 0;
				while ((i < hash1.Length) && (hash1[i] == hash2[i]))
				{
					i += 1;
				}
				if (i == hash1.Length)
				{
					isEqual = true;
				}
			}

			return isEqual;
		}
		#endregion

		#endregion
	}
	#endregion
}